﻿/// <reference path="libraries/jquery-1.4.1.js" />
/// <reference path="Alert.js" />
function CreateUserNameHtml(item, objeto) {
    try {
        $(item).hide();
        $("input[id*='PacienteId']").val(objeto.PacienteId);
        $(item).parent().append(
                        "<div id='dvNomeAtirador' style=\"padding-right:75px\">" +
                            "<span>" +
                                objeto.Nome +
                            "</span>" +
                            "<a href='javascript:RemoverUserNameHtml(\"#dvNomeAtirador\")' >" +
                                "<img src='" + eS.UrlPadrao + "Recursos/i/excluir.png' id='imgExcluir' style='margin-left: 20px; width:15px; height:15px; positon: static; border-style:none; border-color:transparent; border-width: 0px'/>" +
                            "</a>" +
                        "</div>"
                    );
        $("input[id*='Nome']").val(objeto.Nome);
    } catch (e) {

    }
}
function RemoverUserNameHtml(div) {
    var item = $("input[id*='Cpf']");
    item.val("");
    item.show();
    item.parent().children(div).remove();
    $("input[id*='PacienteId']").val("");
    $("input[id*='Nome']").val("");
}
$(function () {
    if ($("input[id*='Nome']").val().length > 0) {
        CreateUserNameHtml("input[id*='Cpf']", { PacienteId: $("input[id*='PacienteId']").val(), Nome: $("input[id*='Nome']").val() });
    }
    $("input[id*='Cpf']").focusout(function () {
        var item = $(this);
        $("input[id*='PacienteId']").val("");
        var alerta = new Alert();
        alerta.Name = "erroGeral";
        $.ajax({
            url: eS.UrlPadrao + "Views/Cadastros/Agendamento.aspx/GetPaciente",
            type: "post",
            data: "{ cpf: '" + $(this).val() + "'}",
            dataType: "json",
            contentType: 'application/json; charset=utf-8',
            success: function (e) {
                if (!e.d || e.d == null || e.d == "null") {
                    alerta.Type = AlertType.Error;
                    alerta.AddMessage("Nada foi encontrado para o cpf informado");
                    alerta.Show();
                }
                else {
                    CreateUserNameHtml(item, e.d);
                }
            },
            error: function (e) {
            }
        });
    });
    $("select[id*='EspecialidadeId']").change(function (e) {
        val = $(this).children("option:selected").val();
        $.ajax({
            url: eS.UrlPadrao + "Views/Cadastros/Agendamento.aspx/GetMedicos",
            type: "post",
            data: "{ especialidadeId:" + val + "}",
            dataType: "json",
            contentType: 'application/json; charset=utf-8',
            success: function (e) {
                try {
                    if (!e.d || e.d == [] || e.d.lenght == 0)
                        return;
                    $("select[id*='MedicoId']").children("option").remove();
                    $("select[id*='MedicoId']")[0].options.add(new Option("Selecione", "0"));
                    for (var i in e.d) {
                        var item = e.d[i];
                        $("select[id*='MedicoId']")[0].options.add(new Option(item.Pessoa.Nome, item.MedicoId));
                    }
                } catch (e) {
                    alert(e);
                }
            },
            error: function (e) {
                if (e.responseText)
                    alert(e.responseText);
                else
                    alert(e);
            }
        });
    });
});